﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace blackhouse.BlackApart.Data {
    public abstract class AdoActionDataAccess : IActionDataAccess, IPushMailDataAccess {

        #region Fields
        #endregion

        #region Methods

        public abstract void SaveLog(int logId, DateTime stardDate, DateTime endDate, string status, string title, string message);

        public abstract void InvokeAutomaticStatusChange();

        public abstract void DeleteInterruptedReservations();

        public abstract void MarkAsActive(string stockActionId);

        public abstract void SavePushMail(string content, bool isHtml, string title, string toMails);

        public PushMailData GetPushMail(int id) {
            return this.ReadPushMailAction(this.GetReaderForPushMailAction(id));
        }

        public abstract void PushMailSended(int id);

        public ActionData DequeueTopAction(string language) {
            return this.ReadAction(this.GetReaderForDequeueTopAction(language));
        }

		protected abstract IDataReader GetReaderForDequeueTopAction(string language);

        protected abstract IDataReader GetReaderForPushMailAction(int id);

        private ActionData ReadAction(IDataReader reader) {
            ActionData data = default(ActionData);
            if (reader.Read())
                data = this.ReadActionFields(reader);
            reader.Close();
            return data;
        }

        private ActionData ReadActionFields(IDataReader reader) {
            ActionData data = new ActionData();
            data.ClassDeclaration = Convert.ToString(reader["ClassDeclaration"]);
            if (reader.IsDBNull(reader.GetOrdinal("InitializeData")))
                data.InitializeData = "";
            else
                data.InitializeData = Convert.ToString(reader["InitializeData"]);
            data.StockActionId = Convert.ToString(reader["StockActionId"]);
            return data;
        }

        private PushMailData ReadPushMailAction(IDataReader reader) {
            PushMailData data = default(PushMailData);
            if (reader.Read())
                data = this.ReadPushMailActionFields(reader);
            reader.Close();
            return data;
        }

        private PushMailData ReadPushMailActionFields(IDataReader reader) {
            PushMailData data = new PushMailData();
            data.Id = Convert.ToInt32(reader["PushMailId"]);
            data.Content = Convert.ToString(reader["Content"]);
            data.IsHtml  =Convert.ToBoolean(reader["IsHtml"]);
            data.Title=Convert.ToString(reader["Title"]);
            data.ToMails = Convert.ToString(reader["To"]);
            return data;
        }

        #endregion

    }
}
